#include "headfile/restcontrol.h"
#include "headfile/myhelper.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlError>
#include <QDebug>
#include <QHash>

restcontrol::restcontrol()
{
}

restcontrol::~restcontrol()
{
}

/*
 * return value: int 1 用户名或者密码错误 -1 登录失败  0 验证成功 2 用户名不存在
 * checkUserAndPass 检验用户名和密码
 */
int restcontrol::checkUserAndPass(QString &username, QString &password)
{
    int status = -1;
    QString sUsername;
    QString sPassword;
    QSqlQuery query;
    bool rtn = query.exec(
                QString("select username,password from user where username = '%1' ").arg(username));
    if(rtn){
        if(query.next()){
            sUsername = query.value(0).toString();
            sPassword = query.value(1).toString();
            if(username == sUsername && password == sPassword) {
                status = 0;
            } else {
                status = 1;
            }
        } else {
            status = 2;
            qDebug() << "checkUserAndPass: "<< "未查到用户名["<<username<<"]";
        }
    } else {
        QSqlError error = query.lastError();
        qDebug() << "checkUserAndPass: "<< error.text();
        status = -1;
    }
    return status;
}

/*
 * 获取餐桌区域类型
 */
void restcontrol::getTableAreaType(QMap<int, QString> *areatypeMap)
{
    int AreaTypeID;
    QString AreaTypeName;
    QSqlQuery query;

    bool rtn = query.exec(
                QString("select AreaTypeID, AreaTypeName from rest_tableareatype "));
    if(rtn){
        while(query.next()){
            AreaTypeID = query.value(0).toInt();
            AreaTypeName = query.value(1).toString().trimmed();
            areatypeMap->insert(AreaTypeID, AreaTypeName);
        }
    } else {
        QSqlError error = query.lastError();
        qDebug() << "getTableAreaType: "<< error.text();
    }
}

/*
 * 获取所有餐桌 roomType = 0 表示所有餐台
 */
void restcontrol::getTables(QSqlQueryModel *sqlModTables, int roomType)
{
    if(roomType == 0){
        sqlModTables->setQuery(QString("SELECT TableID, TableName, TableCode, TypeID, "
                           "TableAreaId, areaType, Capability, DiningStatus, DiningBillID "
                           "FROM rest_diningtable"));
    } else {
        sqlModTables->setQuery(QString("SELECT TableID, TableName, TableCode, TypeID, "
                           "TableAreaId, areaType, Capability, DiningStatus, DiningBillID "
                           "FROM rest_diningtable where areatype = %1").arg(roomType));
    }
}

/*
 * 加载区域名称
 */
void restcontrol::getAreaName(QMap<int, QString> *areaNameMap)
{
    int AreaID;
    QString AreaName;
    QSqlQuery query;

    bool rtn = query.exec(
                QString("select AreaID, AreaName from rest_tablearea "));
    if(rtn){
        while(query.next()){
            AreaID = query.value(0).toInt();
            AreaName = query.value(1).toString().trimmed();
            areaNameMap->insert(AreaID, AreaName);
        }
    } else {
        QSqlError error = query.lastError();
        qDebug() << "getAreaName: "<< error.text();
    }
}

/*
 * 查询单桌信息
 */
void restcontrol::getOneTableInfo(QSqlRecord *record, int tableid)
{
    QSqlQuery tableQuery;
    bool rtn = tableQuery.exec(
            QString("SELECT * "
            "FROM rest_diningtable where TableID = %1").arg(tableid));
    if(rtn){
        tableQuery.next();
        *record = tableQuery.record();
    } else {
        QSqlError error = tableQuery.lastError();
        qDebug() << "getOneTableInfo: "<< error.text();
    }
}

/*
 * 获取订单信息
 */
void restcontrol::getBillInfo(QString *billId, QSqlQueryModel *sqlModBill)
{
    sqlModBill->setQuery(QString("SELECT TableID, TableName, TableCode, TypeID, "
                       "TableAreaId, areaType, Capability, DiningStatus, DiningBillID "
                       "FROM rest_diningtable where areatype = '%1'").arg(*billId));
}

